var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require("express-session");
var canvas = require('canvas');
const axios = require('axios');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var fileRouter = require('./routes/file');
var captchaRouter = require('./routes/captcha');
var showStudentDataRouter = require('./routes/showStudentData');
var changePasswordRouter = require('./routes/changePassword');
var showActivityRouter = require('./routes/showActivity');
var joinRouter = require('./routes/join');
var addActivityRouter = require('./routes/addActivity');
var addManagerRouter = require('./routes/addManager');
var showSGDataRouter = require('./routes/showSGData');
var auditStudentRouter = require('./routes/auditStudent');
var teacherRouter = require('./routes/teacher');
var formRouter = require('./routes/form');var showMDataRouter = require('./routes/showMData');
var seeStudentRouter = require('./routes/seeStudent');
var seeSuggestionRouter = require('./routes/seeSuggestion');
var editActivityRouter = require('./routes/editActivity');
var auditActivityRouter = require('./routes/auditActivity');

// var emailRouter = require('./routes/email');
// var dbRouter = require('./routes/db');

var app = express();
const multer  = require("multer")
const storage = multer.memoryStorage() // or use disk storage
const upload = multer({ storage })
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(session({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}))
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));



app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/file', fileRouter);
app.use('/captcha', captchaRouter);
app.use('/changePassword', changePasswordRouter);
app.use('/showActivity', showActivityRouter);
app.use('/showStudentData', showStudentDataRouter);
app.use('/join', joinRouter);
app.use('/addActivity', addActivityRouter);
app.use('/addManager', addManagerRouter);
app.use('/showSGData', showSGDataRouter);
app.use('/auditStudent', auditStudentRouter);
app.use('/teacher', teacherRouter);
app.use('/form', formRouter);
app.use('/showMData',showMDataRouter);
app.use('/seeStudent',seeStudentRouter);
app.use('/seeSuggestion',seeSuggestionRouter);
app.use('/editActivity',editActivityRouter);
app.use('/auditActivity',auditActivityRouter);

// app.use('/db', dbRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  console.log(err.message);
  res.render('error');
});

module.exports = app;

//session
app = express();
var session = require('express-session');
app.use(session({
}));